package com.office.personalOffice.workSchedule.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.personalOffice.workSchedule.dao.IWorkSchedule;
import com.office.personalOffice.workSchedule.model.OaAgent;
import com.office.personalOffice.workSchedule.model.OaReader;
import com.office.personalOffice.workSchedule.model.OaSchedule;
import com.office.personalOffice.workSchedule.service.IWorkScheduleService;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WorkScheduleService implements IWorkScheduleService {

    @Autowired
    private IWorkSchedule workSchedule;

    public IWorkSchedule getWorkSchedule() {
        return workSchedule;
    }

    public void setWorkSchedule(IWorkSchedule workSchedule) {
        this.workSchedule = workSchedule;
    }

    //查找指定用户的日程安排
    @Override
    public List<OaSchedule> getScheduleByCondition(OaSchedule oaSchedule) {
        return workSchedule.getScheduleByCondition(oaSchedule);
    }

    //添加日程安排
    @Override
    public boolean addSchedule(OaSchedule oaSchedule) {
        return workSchedule.addSchedule(oaSchedule)>0;
    }

    //检查时间（"整个日程安排盖住时间区间 而不是交叉的情况"）
    @Override
    public boolean CheckTime(OaSchedule oaSchedule) {

        boolean flag=false;
        List<OaSchedule> scheduleByCondition = getScheduleByCondition(oaSchedule);
        if(scheduleByCondition.size()!=0){
            flag = false;
        }else{
            List<OaSchedule> oaSchedules = workSchedule.CheckTime(oaSchedule);
            if(oaSchedules.size()!=0){
                flag=false;
            }else {
                flag=true;
            }
        }
        return flag;
    }

    //删除（更新）日程安排状态
    @Override
    public Integer deleteSchedule(OaSchedule oaSchedule) {
        return workSchedule.deleteSchedule(oaSchedule);
    }

    //修改日程安排状态
    @Override
    public Integer updateSchedule(OaSchedule oaSchedule) {
        return workSchedule.updateSchedule(oaSchedule);
    }

    //把查询得到的数据进行分页
    public PageModel<OaSchedule> getScheduleListAfterPage(PageModel<OaSchedule> pageModel,OaSchedule oaSchedule){

        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?1:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<OaSchedule> scheduleByCondition = getScheduleByCondition(oaSchedule);
        PageInfo<OaSchedule> warehousePageInfo = new PageInfo<OaSchedule>(scheduleByCondition);
        pageModel.setData(new ArrayList<OaSchedule>(scheduleByCondition));
        pageModel.setTotalPage(warehousePageInfo.getPages());
        pageModel.setTotalRows(warehousePageInfo.getTotal());
        pageModel.setFirstPage(pageModel.getPageNum()==1?true:false);
        pageModel.setLastPage(pageModel.getPageNum()==pageModel.getTotalPage()?true:false);
        int[] intarr =new int[pageModel.getTotalPage()];
        for (int i =1 ;i<=pageModel.getTotalPage(); i++){
            intarr[i-1]=i;
        }
        pageModel.setNavigatepageNums(intarr);
        return pageModel;

    }

    //查看读者组
    @Override
    public List<OaReader> getReaderByCondition(OaReader oaReader) {
        return workSchedule.getReaderByCondition(oaReader);
    }

    //插入读者组
    @Override
    public Integer addReader(OaReader oaReader) {
        return workSchedule.addReader(oaReader);
    }

    //更新读者组
    @Override
    public Integer udpateReader(OaReader oaReader) {
        return workSchedule.udpateReader(oaReader);
    }

    //查看代办组
    @Override
    public List<OaAgent> getAgentByCondition(OaAgent oaAgent) {
        return workSchedule.getAgentByCondition(oaAgent);
    }

    //查看可以代办的人员
    @Override
    public List<OaAgent> getAgentedByCondition(OaAgent oaAgent) {
        return workSchedule.getAgentedByCondition(oaAgent);
    }

    //插入代办组
    @Override
    public Integer addAgent(OaAgent oaAgent) {
        return workSchedule.addAgent(oaAgent);
    }

    //更新代办组
    @Override
    public Integer udpateAgent(OaAgent oaAgent) {
        return workSchedule.udpateAgent(oaAgent);
    }
}
